<?php
/**
 * Created by PhpStorm.
 * User: gaochaolyf
 * Date: 16/5/23
 * Time: 上午10:44
 */

namespace api\Model;

use Pingpp\Object;
use Think\Model;

class NewsEntityModel extends Model
{
    function __construct() {
        $this->model = M('NewsEntity');
        $this->model1 = M('SysMsg');
        $this->model2 = M('HzResource');
        $this->model3 = M('HzRedetail');
        $this->model4 = M('JobEntity');
    }

    public function getNewsList($field = array('*'), $condition = array(), $page = 1, $pergage = 10, $order = 'sort asc') {
        $data = $this->model->where($condition)->field($field)->page($page, $pergage)->order($order)->select();
        $data = $data ? $data : array();
        return $data;
    }


    public function getjoblist($page,$pergage) {
        $data = $this->model4->page($page, $pergage)->order('sort asc')->select();
        $data = $data ? $data : array();
        return $data;
    }


    public function getOneNews($field = array('*'), $condition = array()) {
        $data = $this->model->where($condition)->field($field)->find();
        $data = $data ? $data : array();
        return $data;
    }

   //会长相册，视频
    public function gethzpicvideo($field = array('*'), $condition = array()) {
        $data = $this->model2->where($condition)->field($field)->select();
        $data = $data ? $data : array();
        return $data;
    }


    //会长相册，视频详情
    public function gethzpicinfo($field = array('*'), $condition = array()) {
        $data = $this->model3->where($condition)->field($field)->select();
        $data = $data ? $data : array();
        return $data;
    }


  //系统消息
    public function getsysmsg($type,$cust_id) {
        $data = $this->model1->where("type='".$type."' and cust_id in(0,".$cust_id.")")->field('*')->order('add_at desc')->select();
        $data = $data ? $data : array();
        return $data;
    }


  //系统消息 第一次
    public function getsysmsg_ft($cust_id) {

        $data_sys = $this->model1->where('type=1')->order('read_or asc,add_at desc')->limit(1)->field('*')->select();

        $data_sys = $data_sys ? : array();

        $data_dd = $this->model1->where('type=2 and cust_id='.$cust_id)->order('add_at desc')->limit(1)->field('*')->select();

        $data_dd = $data_dd ?  : array();

        $no_read_num= $this->model1->where('type=2 and cust_id='.$cust_id.' and read_or=0')->field('sys_msg_id')->count();

        $data=array('sys'=>$data_sys,'dd'=>$data_dd,'no_read_num'=>$no_read_num);
        $data = $data ? : array();
        return $data;
    }

    //系统消息详情

    public function sysmsgdetails($condition) {
        $data = $this->model1->where($condition)->find();
        if($data['read_or']==0){
            $this->model1->where($condition)->save(array('read_or'=>'1'));
        }
        $data = $data ? $data : array();
        return $data;
    }
}